// 时间格式化
export function formatTime(timestamp, format = 'HH:mm') {
  const date = new Date(timestamp);

  const pad = n => n.toString().padStart(2, '0');

  const maps = {
    'YYYY': date.getFullYear(),
    'MM': pad(date.getMonth() + 1),
    'DD': pad(date.getDate()),
    'HH': pad(date.getHours()),
    'mm': pad(date.getMinutes()),
    'ss': pad(date.getSeconds())
  };

  return format.replace(/(YYYY|MM|DD|HH|mm|ss)/g, match => maps[match]);
}

// 本地存储管理
export const storage = {
  get(key) {
    try {
      return JSON.parse(uni.getStorageSync(key));
    } catch(e) {
      return null;
    }
  },

  set(key, value) {
    uni.setStorageSync(key, JSON.stringify(value));
  }
};
